#include "SDLHScrollBar.h"

SDLHScrollBar::SDLHScrollBar(int handle, SDL_Rect dst, SDL_Rect upButton, SDL_Rect downButton,
							 SDL_Rect bg, SDL_Rect sliderTop, SDL_Rect sliderBot, SDL_Rect sliderCenter,
							 const char *fileName, SDLObject *parent, SDL_Surface *sParent, SDL_Event *event) {
	m_Handle = handle;
	m_pParent = parent;
	m_Parent = sParent;
	m_Event = event;
	m_Dst = dst;
	SDL_Surface *load = load_image(fileName);
	SDL_Surface *temp = SDL_CreateRGBSurface(SDL_SWSURFACE | SDL_SRCALPHA, 20, 420, 32, 0, 0, 0, 0);
	apply_surface_from(0, 0, load, &bg, temp);
	m_bg = SDL_DisplayFormat(temp);
	SDL_FreeSurface(temp);
	SDL_Rect tempRect, tempRect2;
	tempRect.x = dst.x;
	tempRect.y = dst.y;
	tempRect.w = upButton.w;
	tempRect.h = upButton.h;
	tempRect2.x = upButton.x;
	tempRect2.y = upButton.y + upButton.h;
	tempRect2.h = upButton.h;
	tempRect2.w = upButton.w;
	m_upButton = new SDLButton(HUPBUTTON, tempRect, upButton, tempRect2, m_Event, sParent, this, fileName);
	tempRect.x = dst.x;
	tempRect.y = dst.y + (dst.h - downButton.h);
	tempRect.w = downButton.w;
	tempRect.h = downButton.h;
	tempRect2.x = downButton.x;
	tempRect2.y = downButton.y + downButton.h;
	tempRect2.h = downButton.h;
	tempRect2.w = downButton.w;
	m_downButton = new SDLButton(HDOWNBUTTON, tempRect, downButton, tempRect2, m_Event, sParent, this, fileName);
}

void SDLHScrollBar::display() {
	apply_surface(m_Dst.x, m_Dst.y + 20, m_bg, m_Parent);
	m_upButton->inputHandler();
	m_upButton->display();
	m_downButton->inputHandler();
	m_downButton->display();
}

void SDLHScrollBar::doAction(int handle, int message) {
	switch(handle) {
	case HUPBUTTON:
		if(message == SDLBUTTONDOWN) printf("Up button pressed\n");
		else if(message == SDLBUTTONUP) m_pParent->doAction(HSCROLLBAR, SCROLLBARUP);
		else printf("Some other action happened to the up button\n");
		break;
	case HDOWNBUTTON:
		if(message == SDLBUTTONDOWN) printf("Down button pressed\n");
		else if(message == SDLBUTTONUP) m_pParent->doAction(HSCROLLBAR, SCROLLBARDOWN);
		else printf("Some other action happened to the down button\n");
	}
}